//
//  PersonChannelViewController.m
//  DemoV2
// ? Controller中逻辑过多怎么办？分类吗？
//  Created by 董一龙 on 2022/6/25.
//

#import "PersonChannelViewController.h"


// delegate部分的分类
#import "Delegate/PersonChannelViewController.h"
#import "DataSource/PersonChannelViewController.h"


@interface PersonChannelViewController ()

@end

@implementation PersonChannelViewController
- (instancetype)init{
    if(self = [super init]){
        self.tabBarItem.title = @"我的";
        self.tabBarItem.image = kGetImage(@"shutiao.png");
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.personChannelModel = [PersonChannelModel new];
    
    [self initUI];
}

- (void)initUI{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:({
        self.personChannelView = [[PersonChannelView alloc] initWithFrame:self.view.frame];
        self.personChannelView;
    })];
    
    self.personChannelView.collectionView.dataSource = self;
    self.personChannelView.collectionView.delegate = self;
    [self.personChannelView.collectionView registerClass:[PersonCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([PersonCollectionViewCell class])];
    [self.personChannelView.collectionView registerClass:[PersonCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PersonCollectionHeaderView class])];
    
    // 点击editbutton，第一个section内cell出现删除按钮，并且出现抖动动画。
    // 此时所有cell可以响应点击, 第二个section中点击cell后移动到第一个section，第一个section点击cell的删除按钮移动到第二个section
    // 第一个cell中点击拖拽可以中第一一个section中排序移动
    [self.personChannelView.editButton addTarget:self action:@selector(clickEditButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickEditButton:(UIButton *)button{
    self.isEditState = !self.isEditState;
    NSString *title = self.isEditState ? @"完成" : @"编辑";
    [self.personChannelView.editButton setTitle:title forState:UIControlStateNormal];
    for(PersonCollectionViewCell *cell in self.personChannelView.collectionView.visibleCells){
        NSIndexPath *indexPath = [self.personChannelView.collectionView indexPathForCell:cell];
        if(self.isEditState && indexPath.section == 0) {
            [cell beginAnimation];
            // 添加点击按钮的事件
            [cell.cancelButton addTarget:self action:@selector(clickCellCancelButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        else{
            [cell stopAnimation];
        }
    }
}

- (void)clickCellCancelButton:(UIButton *)button{
    NSLog(@"点击到cell内删除按钮");
    // cell -> button
    PersonCollectionViewCell *cell = (PersonCollectionViewCell *)[button superview];
    NSIndexPath *indexPath = [self.personChannelView.collectionView indexPathForCell:cell];
    NSInteger dstArraySize = self.personChannelModel.allArray[1].count;
    NSInteger insertItemIndex = dstArraySize;
    
    // 1. 更新数据源
    NSLog(@"更新数据源");
    ItemModel * item = self.personChannelModel.allArray[indexPath.section][indexPath.item];
    [self.personChannelModel.allArray[indexPath.section] removeObjectAtIndex:indexPath.item];
    [self.personChannelModel.allArray[1] insertObject:item atIndex:insertItemIndex];
    
    // 2. 通知UI更新
    NSLog(@"更新UI");
    NSIndexPath *dstIndexPath = [NSIndexPath indexPathForItem:insertItemIndex inSection:1];
    NSLog(@"%ld %ld -> %ld %ld", indexPath.section, indexPath.item, dstIndexPath.section, dstIndexPath.item);
    [self.personChannelView.collectionView moveItemAtIndexPath:indexPath toIndexPath:dstIndexPath];
    
    
    // 3. 取消抖动动画并隐藏删除按钮
    NSLog(@"取消动画隐藏按钮");
    PersonCollectionViewCell *dstcell = (PersonCollectionViewCell *)[self.personChannelView.collectionView cellForItemAtIndexPath:dstIndexPath];
    // 踩坑，不小心移除了collectionview自带的动画
    [dstcell stopAnimation]; // 开启动画时顺便开启了按钮
    
    // 4. 移除点击按钮的事件
    NSLog(@"移除事件");
    [dstcell.cancelButton removeTarget:self action:@selector(clickCellCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"OK");
}
@end
